package com.example.md.mddemo.services;

import com.example.md.mddemo.entity.File;
import com.example.md.mddemo.repo.FileRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Copyright © 2018年 ziniuxiaozhu. All rights reserved.
 *
 * @Author 临江仙 hzqiuxm@163.com
 * TODO:亲爱的临江仙，请写点注释吧...........
 * @Date 2018/8/20 15:23
 */
@Slf4j
@Service
public class FileService {

    @Autowired
    public FileRepository fileRepository;

    public File saveFile(File file) {
        return fileRepository.save(file);
    }

    public void removeFile(String id) {
        fileRepository.deleteById(id);
    }

    public Optional<File> getFileById(String id) {
        return fileRepository.findById(id);
    }


    public List<File> listFilesByPage(int pageIndex, int pageSize) {
        String msg = "";

        if (log.isDebugEnabled()){
            log.debug("The debug messgae is : {}",msg);
        }

        Page<File> page = null;
        List<File> list = null;

        Sort sort = new Sort(Sort.Direction.DESC,"uploadDate");
        Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

        page = fileRepository.findAll(pageable);
        list = page.getContent();
        return list;



    }


}
